/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.niceshot;

import java.util.ArrayList;
import java.util.Map;

/**
 *
 * @author Alexandre
 */
public class DataToJson {

    public String HashMapToJson(String nmObject, Map resultHash) {

        Object[] arrayKeys = resultHash.keySet().toArray();
        Object[] arrayValues = resultHash.values().toArray();
        String resultJson = "\"" + nmObject + "\" : {";

        for (int i = 0; i < resultHash.size(); i++) {
            if (i > 0) {
                resultJson += ",";
            }
            resultJson += "\"" + String.valueOf(arrayKeys[i]) + "\"";
            resultJson += " : \"" + String.valueOf(arrayValues[i]) + "\"";
        }
        resultJson += "}";

        return resultJson;
    }

    public String ArrayObjectHashMapToJson(String titleObj, ArrayList<Object> arrayObj) {
        String resultJson = resultJson = "\"" + titleObj + "\" : {";
        if (arrayObj.size() > 0) {
            resultJson = "\"" + titleObj + "\" : {";
            for (int i = 0; i < arrayObj.size(); i++) {
                resultJson += HashMapToJson(String.valueOf(i + 1), (Map) arrayObj.get(i));
                if (i == arrayObj.size() - 1) {
                    resultJson += "}";
                } else {
                    resultJson += ",";
                }
            }
        } else {
            resultJson += "}";
        }

        return resultJson;
    }

}
